package com.DHH.Controller.Order;

import com.DHH.Mapper.Order.OrderInfoStateMapper;
import com.DHH.Dto.Message;
import com.DHH.Dto.PageInfo;
import com.DHH.Dto.QueryInfo;
import com.DHH.Entity.Order.OrderInfoState;
import com.DHH.Service.Order.IOrderInfoStateService;
import com.DHH.Util.ReflectionUtils;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.validation.BindingResult;
import org.springframework.validation.FieldError;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import tk.mybatis.mapper.entity.Example;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import javax.validation.Valid;
import java.util.List;

import javax.servlet.http.HttpSession;


/**
 * Created by Administrator on 2017/4/15.
 */
@Controller
@RequestMapping("Admin/Order/OrderInfoState")
public class OrderInfoStateController {
    private Logger logger = LoggerFactory.getLogger(OrderInfoStateController.class);
    @Autowired
    private OrderInfoStateMapper orderInfoStateMapper;
    @Autowired
    private IOrderInfoStateService orderInfoStateService;

    @RequestMapping("/listPage")
    public String listPage(HttpSession session, ModelMap modelMap) {
        return "admin/orderInfoState/listOrderInfoStatePage.jsp";
    }

    @RequestMapping("/addPage")
    public String addPage(HttpSession session, ModelMap modelMap) {
        return "admin/orderInfoState/addOrderInfoStatePage.jsp";
    }

    @RequestMapping("/updatePage")
    public String updatePage(HttpSession session, @RequestParam Integer id, ModelMap modelMap) {
        return "admin/orderInfoState/update{className}Page.jsp";
    }

    @RequestMapping("/detailsPage")
    public String detailPage(HttpSession session, @RequestParam Integer id, ModelMap modelMap) {
        return "admin/orderInfoState/detailsOrderInfoStatePage.jsp";
    }

    @RequestMapping("/add")
    @ResponseBody
    public Message add(@Valid OrderInfoState orderInfoState, BindingResult result) {
        Message msg = new Message();
        if (result.hasErrors()) {
            List<FieldError> err = result.getFieldErrors();
            for (FieldError fe : err) {
                msg.addError(fe.getField().toString(), fe.getDefaultMessage());
            }
            return msg;
        }

        try {
            orderInfoState = orderInfoStateService.insertOrderInfoState(orderInfoState);
            msg.setMsg("添加成功!");
            msg.setData(orderInfoState);
            msg.setState(true);
            return msg;
        } catch (Exception e) {
            msg.setMsg("操作失败!");
            return msg;
        }
    }

    @RequestMapping("/saveOrUpdate")
    @ResponseBody
    public Message saveOrUpdate(@Valid OrderInfoState orderInfoState, BindingResult result) {
        Message msg = new Message();
        if (result.hasErrors()) {
            List<FieldError> err = result.getFieldErrors();
            for (FieldError fe : err) {
                msg.addError(fe.getField().toString(), fe.getDefaultMessage());
            }
            return msg;
        }

        try {
            orderInfoState = orderInfoStateService.saveOrUpdateOrderInfoState(orderInfoState);
            msg.setMsg("添加成功!");
            msg.setData(orderInfoState);
            msg.setState(true);
            return msg;
        } catch (Exception e) {
            msg.setMsg("操作失败!");
            return msg;
        }
    }

    @RequestMapping("/delete")
    @ResponseBody
    public Message delete(@RequestParam Integer id) {
        Message msg = new Message();
        try {
            OrderInfoState orderInfoState = new OrderInfoState();
            orderInfoState.setId(id);
            orderInfoState = orderInfoStateService.deleteOrderInfoState(orderInfoState);
            msg.setMsg("操作成功");
            msg.setState(true);
            msg.setData(orderInfoState);
            return msg;
        } catch (Exception e) {
            msg.setMsg("操作失败");
            return msg;
        }
    }

    @RequestMapping("/update")
    @ResponseBody
    public Message update(OrderInfoState orderInfoState) {
        Message msg = new Message();
        try {
            msg.setData(orderInfoStateService.updateOrderInfoState(orderInfoState));
            msg.setMsg("操作成功！");
            msg.setState(true);
            return msg;
        } catch (Exception e) {
            msg.setMsg("操作失败，请稍后重试");
            return msg;
        }
    }

    @ResponseBody
    @RequestMapping("/query")
    public PageInfo<OrderInfoState> query(QueryInfo queryInfo) {
        System.err.println("--------------查询参数---------------");
        System.err.println(queryInfo);
        System.err.println("--------------查询参数---------------");
        /*为查询设置统一分页条件和排序条件*/
        PageHelper.startPage(queryInfo.getCurrentPage(), queryInfo.getPageSize());
        PageHelper.orderBy(queryInfo.getSort() + " " + queryInfo.getOrder());
        if (queryInfo.getType() == null) {
            queryInfo.setType("search");
        }
        if (queryInfo.getCurrentPage() == null) {
            queryInfo.setCurrentPage(1);
        }
        if (queryInfo.getPageSize() == null) {
            queryInfo.setPageSize(10);
        }
        try {
            if (queryInfo.getType().equals("screen")) {
                return this.screen(queryInfo);
            }
            if (queryInfo.getType().equals("filter")) {

                return this.searchFilter(queryInfo);
            }
            return this.search(queryInfo);
        } catch (Exception e) {
            PageInfo<OrderInfoState> p = new PageInfo<OrderInfoState>();
            p.setError(e.getMessage());
            p.setMsg("查询失败，请稍后重试！");
            return p;
        }

    }


    /**
     * 查询
     *
     * @param queryInfo
     * @return
     */
    private PageInfo<OrderInfoState> search(QueryInfo queryInfo) {
        System.out.println("---------\n" + queryInfo);

        OrderInfoState orderInfoState = new OrderInfoState();
            /*在这里设置相关的查询参数*/
        PageInfo<OrderInfoState> pageInfo = new PageInfo<OrderInfoState>();
        Page<OrderInfoState> page = (Page<OrderInfoState>) orderInfoStateMapper.searchByOrderInfoState(orderInfoState);
        pageInfo.setRows(page.getResult());
        pageInfo.setTotalRow(page.getTotal());
        pageInfo.setCurrentPage(queryInfo.getCurrentPage());
        return pageInfo;
    }

    /**
     * 筛选
     *
     * @param queryInfo
     * @return
     */
    private PageInfo<OrderInfoState> screen(QueryInfo queryInfo) {
        PageInfo<OrderInfoState> pageInfo = new PageInfo<OrderInfoState>();
        OrderInfoState orderInfoState = new OrderInfoState();
        for (String key : queryInfo.getMap().keySet()) {
            ReflectionUtils.setFieldValue(orderInfoState, key, queryInfo.getMap().get(key));
        }
        Page<OrderInfoState> page = (Page<OrderInfoState>) orderInfoStateMapper.screenByOrderInfoState(orderInfoState);
        pageInfo.setRows(page.getResult());
        pageInfo.setTotalRow(page.getTotal());
        pageInfo.setCurrentPage(queryInfo.getCurrentPage());
        return pageInfo;
    }

    /**
     * 过滤
     *
     * @param queryInfo
     * @return
     */
    private PageInfo<OrderInfoState> searchFilter(QueryInfo queryInfo) {
        OrderInfoState orderInfoState = new OrderInfoState();
        PageInfo<OrderInfoState> pageInfo = new PageInfo<OrderInfoState>();
        if (queryInfo.getFilterType() == null || queryInfo.getFilterType().length() == 0) {
            queryInfo.setFilterType("number");
        }
        if (queryInfo.getFilterType().equals("number")) {
            //类强制转换并设置值进行操作
        }
        Page<OrderInfoState> page = (Page<OrderInfoState>) orderInfoStateMapper.filterByOrderInfoState(orderInfoState);
        pageInfo.setRows(page.getResult());
        pageInfo.setTotalRow(page.getTotal());
        pageInfo.setCurrentPage(queryInfo.getCurrentPage());
        return pageInfo;
    }
}

